const { rejects } = require("assert");
let mysql = require("mysql");
const { resolve } = require("path");

var pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'zhixin',
    timezone: "08:00"
});

function doMysql(sql, data = []) {
    return new Promise((resolve, reject) => {
        pool.getConnection(function (error, connection) {
            if (error) {
                return reject(error)
            }
            connection.query(sql, data, (err, success) => {
                if (err) {
                    return reject(err)
                }
                resolve(success)
            })
        })
    })
}

function backText(data, total) {
    return {
        data: data,
        success: true,
        message: "数据请求成功",
        errorCode: 0,
        total: total,
        code: 20000
    }
}

function postText(data) {
    return {
        data: data,
        success: true,
        message: "数据增加成功",
        errorCode: 0,
        code: 20000,
    }
}

function delText(data) {
    return {
        data: data,
        success: true,
        message: "数据删除成功",
        errorCode: 0,
        code: 20000,
    }
}

function handerYes(data) {
    return {
        success: 'true',
        errcode: 0,
        code: 20000,
        msg: '数据请求成功',
        data: data,
    }
}

function fool() {
    return {
        'success': 'true',
        'errorCode': 401,
        'msg': '当前没有数据',
    }
}

//失败
var errFn = (err) => {
    return {
        success: false,
        errcode: 0,
        msg: '数据请求失败',
        data: err,
        code: 0000
    }
}

exports.doMysql = doMysql
exports.backText = backText
exports.postText = postText
exports.delText = delText
exports.handerYes = handerYes
exports.fool = fool
exports.errFn = errFn
